package com.pi.wms.service;

import com.pi.common.utils.StringUtils;
import com.pi.framework.core.repository.IBaseRepository;
import com.pi.framework.core.repository.specification.SimpleSpecificationBuilder;
import com.pi.framework.core.service.impl.BaseCrudService;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.dto.inventory.StorageQueryDto;
import com.pi.wms.entity.TwiInventoryXwmsDetails;
import com.pi.wms.repository.XwmsInventoryDetailsRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Slf4j
@Transactional
@Service
public class XwmsInventoryDetailsService extends BaseCrudService<TwiInventoryXwmsDetails, Long>{

	@Autowired
	protected XwmsInventoryDetailsRepository repository;

    @Override
	protected IBaseRepository<TwiInventoryXwmsDetails,Long> getRepository() {
		return this.repository;
	}
	
	@Override
	protected Predicate toPredicate(Root<TwiInventoryXwmsDetails> root, CriteriaQuery<?> cq, CriteriaBuilder cb, Map<String, ?> query) {
		List<Predicate> list = new ArrayList<Predicate>();

		String divisionid = (String) query.get("divisionid");
		if (!StringUtils.isNullOrBlank(divisionid)) {
			list.add(cb.equal(root.get("divisionId"), divisionid));
		}

		String mat = (String) query.get("material");
        if (!StringUtils.isNullOrBlank(mat)) {
            list.add(cb.or(cb.like(root.get("materialCode"), "%" + mat.trim() + "%"), cb.like(root.get("materialName"), "%" + mat.trim() + "%")));
		}

        String wbs = (String) query.get("wbsnum");
        if (!StringUtils.isNullOrBlank(wbs)) {
            list.add(cb.like(root.get("wbsNum"), "%"+wbs.trim()+"%"));
        }

        String sup = (String) query.get("supplier");
        if (!StringUtils.isNullOrBlank(sup)) {
            list.add(cb.or(cb.like(root.get("supplierCode"), "%" + sup.trim() + "%"), cb.like(root.get("supplierName"), "%" + sup.trim() + "%")));
        }

        String status = (String) query.get("status");
        if (!StringUtils.isNullOrBlank(status)) {
            list.add(cb.equal(root.get("lineStatus"), status.trim()));
        }
		Predicate[] p = new Predicate[list.size()];
		return cb.and(list.toArray(p));
	}

	public List<TwiInventoryXwmsDetails> query(StorageQueryDto query){
		SimpleSpecificationBuilder<TwiInventoryXwmsDetails> sb = new SimpleSpecificationBuilder<TwiInventoryXwmsDetails>();
		if (query!=null){
            if (query.getDivisionId()!=null){
                sb.and("divisionId", "=", query.getDivisionId());
            }
            if (query.getMaterialId()!=null){
                sb.and("materialId", "=", query.getMaterialId());
            }
            if (query.getLotnoId()!=null){
                sb.and("lotnoId", "=", query.getLotnoId());
            }
            if (!StringUtils.isNullOrBlank(query.getLotNumber())){
                sb.and("lotNumber", "=", query.getLotNumber().trim());
            }
            if (!StringUtils.isNullOrBlank(query.getZoneCode())){
                sb.and("zoneCode", "=", query.getZoneCode().trim());
            }
            if (!StringUtils.isNullOrBlank(query.getLocationCode())){
                sb.and("locationCode", "=", query.getLocationCode().trim());
            }
            if ((query.getZoneTypes()!=null)&&(query.getZoneTypes().size()>0)){
                sb.and("zoneType", "in", query.getZoneTypes());
            }
            if ((query.getZoneIds()!=null)&&(query.getZoneIds().size()>0)){
                sb.and("zoneId", "in", query.getZoneIds());
            }
            if ((query.getLocationTypes()!=null)&&(query.getLocationTypes().size()>0)){
                sb.and("locationType", "in", query.getLocationTypes());
            }
            if ((query.getLaneNumber()!=null)&&(query.getLaneNumber()>0)){
                sb.and("laneNumber", "=", query.getLaneNumber());
            }
            if ((query.getDepgth()!=null)&&(query.getDepgth()>0)){
                sb.and("locationDepth", "=", query.getDepgth());
            }
            if (!StringUtils.isNullOrBlank(query.getFullFlag())){
                sb.and("fullFlag", "=", query.getFullFlag().trim());
            }
        }
		Specification<TwiInventoryXwmsDetails> spec = sb.generateSpecification();
		//return this.repository.findAll(spec,Sort.by("paSeqCode"));//
        int pageno=0;
        if ((query.getPageNo()!=null) && (query.getPageNo()>0)){
            pageno=query.getPageNo();
        }
        int pagesize=20000;
        if ((query.getPageSize()!=null) && (query.getPageSize()>0)){
            pagesize=query.getPageSize();
        }
        Pageable pageable = PageRequest.of(pageno, pagesize, Sort.by("lotNumber"));
        List<TwiInventoryXwmsDetails> storages=new ArrayList<>();
        Page<TwiInventoryXwmsDetails> datas=this.repository.findAll(spec, pageable);
        if (datas!=null){
            datas.forEach((x)->{
                storages.add(x);
            });
        }
        return storages;
	}

    /**
     *  清除历史数据
     */
	public String cleanAll(Long divisionId){
        String m = "";
        if (divisionId==null){
            divisionId= XcmgAmConstants.DIVISION_WH101_ID;
        }
        int count=0;
        try{
            count=this.repository.clean(divisionId);
        }catch (Exception e){
            m="清除历史数据异常.异常:"+e.getMessage();
            e.printStackTrace();
        }
        return m;
    }


}
